package com.smartsofa.main;

import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.smartsofa.R;
import com.smartsofa.util.MyUtils;

public class MessagePage extends Activity {
	Map<String,Object> map;
	private TextView familyname;
	private TextView yaoqingname;
	private TextView sign;
	private TextView verifymsg;
	private TextView tv_done;
	
	private Button bt_agree;
	private Button bt_refuse;
	private Button bt_messagepage_back;
	
	private int familyId;
	private ProgressDialog mDialog;
	private Boolean flag = true;
	private String msgFromServer;
	private int verfyState;
	private String msgState;
	
	//定义网络连接超时判定变量
	private boolean network_timeout=false;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_messagepage);
		initView();
		
		Intent intent = getIntent();
//		Bundle bundle = getIntent().getExtras();
		int position = intent.getIntExtra("position",0);
//        SerializableMap serializableMap = (SerializableMap) bundle.get("map");
        Map<String,Object> msgMap = UserMessage.msglist.get(position);
	
		
			String familyname = msgMap.get("familyname").toString();
			this.familyname.setText(familyname);
			String yaoqingname = msgMap.get("username").toString();
			this.yaoqingname.setText(yaoqingname);
			
			msgState = msgMap.get("msgstate").toString();
			if(msgState.equals("邀请你")){
				verfyState = Integer.valueOf((String) msgMap.get("validateStatus"));
				String sign = msgMap.get("sign").toString();
				this.sign.setText(sign);
				String verifymsg = msgMap.get("verifyMsg").toString();
				this.verifymsg.setText(verifymsg);
				familyId = Integer.valueOf( (String) msgMap.get("familyid"));
				msgState = msgMap.get("msgstate").toString();
				if(verfyState==1){
					bt_agree.setVisibility(View.INVISIBLE);
					bt_refuse.setVisibility(View.INVISIBLE);
					tv_done.setVisibility(View.VISIBLE);
					tv_done.setText("已同意加入家庭组");
				}else if (verfyState==2) {
					bt_agree.setVisibility(View.INVISIBLE);
					bt_refuse.setVisibility(View.INVISIBLE);
					tv_done.setVisibility(View.VISIBLE);
					tv_done.setText("已拒绝加入家庭组");
				}else{
					bt_agree.setVisibility(View.VISIBLE);
					bt_refuse.setVisibility(View.VISIBLE);
					tv_done.setVisibility(View.INVISIBLE);
				}
			}else{
				finish();
				
			}
		
		
	}


	private void initView() {
		familyname = (TextView) findViewById(R.id.p_familyname);
		yaoqingname = (TextView) findViewById(R.id.p_yaoqingname);
		sign = (TextView) findViewById(R.id.p_sign);
		verifymsg = (TextView) findViewById(R.id.p_verifyMsg);
		tv_done = (TextView) findViewById(R.id.tv_done);
		bt_agree = (Button) findViewById(R.id.bt_agree);
		bt_refuse = (Button) findViewById(R.id.bt_refuse);
		bt_messagepage_back=(Button)findViewById(R.id.bt_messagepage_back);
		mDialog = new ProgressDialog(MessagePage.this);
		mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		mDialog.setMessage("处理中，请稍候...");
		
		bt_agree.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Thread thread = new Thread(new sendAgreeToServer());
				thread.start();
				mDialog.show();
			}
		});
		bt_refuse.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Thread thread = new Thread(new sendRefuseToServer());
				thread.start();
				mDialog.show();
			}
		});
		bt_messagepage_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				MessagePage.this.finish();
			}
		});
	}
	private class sendAgreeToServer implements Runnable{

		@Override
		public void run() {
			try {	
				String url=MyUtils.getUrl()+"family_approveJoin?userid="+MyUtils.getUSERID()+"&familyid="+familyId;
				HttpGet request = new HttpGet(url);																
		       // 发送请求
			   BasicHttpParams httpParams = new BasicHttpParams();
			   HttpConnectionParams.setConnectionTimeout(httpParams,MyUtils.REQUEST_TIMEOUT);
		       HttpClient httpClient = new DefaultHttpClient(httpParams);
		       HttpResponse httpResponse = httpClient.execute(request);					       
		       	
		      if (httpResponse.getStatusLine().getStatusCode() == 200) {
		    	    network_timeout=true;
			        String jsonString = EntityUtils.toString(httpResponse.getEntity());
			        JSONObject json = new JSONObject(jsonString);
			        flag=!(Boolean) json.get("success");
			        msgFromServer = json.get("msg").toString();
			        Log.i("message", msgFromServer);
			        mDialog.dismiss();
			        		       
		      }
		      }catch (Exception e) {
		        	 e.printStackTrace();
		      }
			Message msg = handler.obtainMessage();
			if(network_timeout){
				if (flag) {				
					msg.what = 1;									
				}else{
					msg.what = 2;			
				}
			}			
			else{
				msg.what = 3;						
			}
			handler.sendMessage(msg);
			}
		
	}
	private class sendRefuseToServer implements Runnable{

		@Override
		public void run() {
			try {	
				String url=MyUtils.getUrl()+"family_rejectJoin?userid="+MyUtils.getUSERID()+"&familyid="+familyId;
				HttpGet request = new HttpGet(url);																
		       // 发送请求
			   BasicHttpParams httpParams = new BasicHttpParams();
			   HttpConnectionParams.setConnectionTimeout(httpParams,MyUtils.REQUEST_TIMEOUT);
		       HttpClient httpClient = new DefaultHttpClient(httpParams);
		       HttpResponse httpResponse = httpClient.execute(request);					       
		       	
		      if (httpResponse.getStatusLine().getStatusCode() == 200) {
			    
			        String jsonString = EntityUtils.toString(httpResponse.getEntity());
			        JSONObject json = new JSONObject(jsonString);
			        flag=!(Boolean) json.get("success");
			        msgFromServer = "已拒绝加入该群";
			        		       
		      }
		      }catch (Exception e) {
		        	 e.printStackTrace();
		      }	
			Message message = handler.obtainMessage();
			if(flag){
				message.what = 1;
			}
			else{
				message.what  = 2;
			}
			handler.sendMessage(message);
		}
		
	}
	Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			mDialog.dismiss();
			switch (msg.what) {
			case 1:
				bt_agree.setVisibility(View.INVISIBLE);
				bt_refuse.setVisibility(View.INVISIBLE);
				tv_done.setVisibility(View.VISIBLE);
				tv_done.setText(msgFromServer);
				break;
			case 2:
				Toast.makeText(MessagePage.this, "处理失败", Toast.LENGTH_LONG).show();
			case 3:
				Toast.makeText(MessagePage.this, "网络连接超时！", Toast.LENGTH_SHORT).show();
				break;
			}
		}
	};
}

